﻿using Microsoft.Extensions.Logging;

namespace Sgr.Sms
{
    /// <summary>
    /// 默认短信服务提供者（仅在控制台输出消息）
    /// </summary>
    public class DefaultSmsProvider : ISmsProvider
    {
        /// <summary>
        /// 服务提供者名称
        /// </summary>
        public string Name => "SGR_SMS_DEFAULT";

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phoneNumber">手机号码</param>
        /// <param name="message">短信内容</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>发送结果</returns>
        public Task<SmsResult> SendAsync(string phoneNumber, string message, CancellationToken cancellationToken = default)
        {
            // 参数验证
            if (string.IsNullOrWhiteSpace(phoneNumber))
                return Task.FromResult(SmsResult.Failure("手机号码不能为空"));

            if (string.IsNullOrWhiteSpace(message))
                return Task.FromResult(SmsResult.Failure("短信内容不能为空"));

            try
            {
                // 在控制台输出短信内容
                var output = $@"""
                    =====================================
                    发送短信:
                    接收号码: {phoneNumber}
                    短信内容: {message}
                    发送时间: UTC {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss}
                    =====================================
                    """;

                Console.WriteLine(output);

                return Task.FromResult(SmsResult.Success());
            }
            catch (Exception ex)
            {
                return Task.FromResult(SmsResult.Failure($"发送失败: {ex.Message}"));
            }
        }
    }
}